/*
 * @LastEditTime: 2023-02-28 14:36:34
 */
import { router } from "../index";
import { MusicList } from "../../Models/MusicList.schema";
import { MusicListController } from "../../Controller/MusicList.controller";
import { createMusicListDTO, updateMusicListDTO } from "../../DTO/MusicList.dto";

const MusicListModel = new MusicListController(MusicList);

//按歌曲名获取歌曲
router.get("/musicList", async (ctx, next) => {
  // 标题查询
  if (ctx.request.query.title) {
    console.log(ctx.request.query.title)
    const res = await MusicListModel.findByTitle(ctx.request.query.title.toString()) as { code: number, message: string };
    res.code === 400 ? ctx.status = 400 : null;
    ctx.body = res;
  }
  // ID 查询
  else if (ctx.request.query.id) {
    ctx.body = await MusicListModel.findById(ctx.request.query.id.toString());
  }
  // 随机查询
  else if (ctx.request.query.random) {
    ctx.body = await MusicListModel.findBySize(Number(ctx.request.query.random));
  }
});
//随机获取歌单
router.get("/musicList/random/:size", async (ctx, next) => {
  ctx.body = await MusicListModel.findBySize(Number(ctx.params.size))
})
// 获取歌单信息
router.get("/musicList/Info", async (ctx, next) => {
  if (ctx.request.query.id) {
    ctx.body = await MusicListModel.getMusicListInfo(ctx.request.query.id.toString())
  }
});
// 创建歌单
router.post('/musicList', async (ctx, next) => {
  console.log(ctx.request.body);
  ctx.body = await MusicListModel.createItem(ctx.request.body as createMusicListDTO);
});

//删除歌单
router.delete("/musicList/:id", async (ctx, next) => {
  ctx.body = await MusicListModel.removeById(ctx.params.id);
});

// 更新歌单
router.patch("/musicList/:id", async (ctx, next) => {
  if (ctx.params.id)
    ctx.body = await MusicListModel.updateById(
      ctx.params.id,
      ctx.request.body as updateMusicListDTO
    );
});

//获取歌单分类
router.get("/musicList/class", async (ctx, next) => {
  ctx.body = await MusicListModel.getClass();
});
//获取歌单列表
router.get("/musicList/list", async (ctx, next) => {
  console.log("进来了")
  if (ctx.request.query.type && ctx.request.query.limit && ctx.request.query.page) {
    ctx.body = await MusicListModel.getList(Number(ctx.request.query.limit), Number(ctx.request.query.page), ctx.request.query.type.toString());
  }
});
//获取全部歌单
router.get("/musicList/:all", async (ctx, next) => {
  ctx.body = await MusicListModel.findAll();
});